<template>
<div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item :label="$t('user.search_field')">
          <el-input
            v-model="formInline.nickName"
            :placeholder="$t('user.input_search_field')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('user.member_level')">
          <el-select v-model="formInline.gradeId" :placeholder="$t('user.please_select')">
            <el-option :label="$t('user.all')" value="0"></el-option>
            <el-option
              v-for="(item, index) in gradeList"
              :key="index"
              :label="item.name"
              :value="item.gradeId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('user.register_time')">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              v-model="formInline.startDate"
              type="date"
              value-format="YYYY-MM-DD"
              :placeholder="$t('user.select_start_date')"
            >
            </el-date-picker>
            <span>-</span>
            <el-date-picker
              v-model="formInline.endDate"
              type="date"
              value-format="YYYY-MM-DD"
              :placeholder="$t('user.select_end_date')"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="onSubmit"
            >{{$t('user.search')}}</el-button
          >
        </el-form-item>
        <!--添加会员-->
        <div class="common-level-rail">
          <el-button
            size="small"
            type="primary"
            icon="Plus"
            v-auth="'/user/user/add'"
            @click="addClick"
            >{{$t('user.create_member')}}</el-button
          >
        </div>
      </el-form>
      <div class="mb16">
        <span>{{$t('user.tags')}}</span>
        <span class="pl10">
          <el-radio-group
            v-model="formInline.tagId"
            size="mini"
            @change="onSubmit"
          >
            <el-radio-button label="0">{{$t('user.all')}}</el-radio-button>
            <el-radio-button
              :label="item.tagId"
              v-for="(item, index) in allTag"
              :key="index"
              :value="item.tagId"
            >
              {{item.tagName}}
            </el-radio-button>
          </el-radio-group>
        </span>
      </div>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            prop="userId"
            label="ID"
            width="80"
          ></el-table-column>
          <el-table-column prop="nickName" :label="$t('user.nickname')"></el-table-column>
          <el-table-column :label="$t('user.wechat_avatar')">
            <template #default="scope">
              <img :src="scope.row.avatarUrl" :width="30" :height="30" />
            </template>
          </el-table-column>
          <el-table-column prop="gradeName" :label="$t('user.member_level')"></el-table-column>
          <el-table-column prop="points" :label="$t('user.points')"></el-table-column>
          <el-table-column prop="balance" :label="$t('user.balance')"></el-table-column>
          <el-table-column prop="mobile" :label="$t('user.phone_number')"></el-table-column>
          <el-table-column prop="refereeName" :label="$t('user.referrer')"></el-table-column>
          <el-table-column
            prop="createTime"
            :label="$t('user.register_time')"
            width="140"
          ></el-table-column>
          <el-table-column fixed="right" :label="$t('user.actions')" width="220">
            <template #default="scope">
              <el-button
                @click="rechargeClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/user/user/recharge'"
                >{{$t('user.recharge')}} 
</el-button>
              <!-- <el-button @click="editClick(scope.row)" type="text" size="small" v-auth="'/user/user/grade'">等级 -->
              <el-button
                @click="editClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/user/user/edit'"
                >{{$t('user.level_label')}} 
</el-button>
              <el-button
                @click="tagClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/user/user/delete'"
                >{{$t('user.tags')}} 
</el-button>
              <el-button
                @click="editBasicClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/user/user/delete'"
                >{{$t('user.edit')}} 
</el-button>
              <el-button
                @click="deleteClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/user/user/delete'"
                >{{$t('user.delete')}} 
</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>

    <!--会员充值-->
    <Recharge
      v-if="open_add"
      :open_add="open_add"
      :form="userModel"
      :gradeList="gradeList"
      @closeDialog="closeDialogFunc($event, 'add')"
    ></Recharge>
    <!--会员等级-->
    <Grade
      v-if="open_edit"
      :open_edit="open_edit"
      :form="userModel"
      :gradeList="gradeList"
      @closeDialog="closeDialogFunc($event, 'edit')"
    ></Grade>
    <!--会员标签-->
    <Tag
      v-if="open_tag"
      :open_tag="open_tag"
      :form="userModel"
      @closeDialog="closeDialogFunc($event, 'tag')"
    ></Tag>
    <!--会员标签-->
    <editUser
      v-if="open_basic"
      :open_basic="open_basic"
      :form="userModel"
      @closeDialog="closeDialogFunc($event, 'basic')"
    ></editUser>
  </div>
</template>

<script>
import UserApi from "@/api/user.js";
import Grade from "./dialog/Grade.vue";
import Recharge from "./dialog/Recharge.vue";
import Tag from "./dialog/Tag.vue";
import editUser from "./dialog/editUser.vue";
export default {
  components: {
    /*编辑组件*/
    Grade,
    Recharge,
    Tag,
    editUser
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        nickName: "",
        gradeId: "",
        tagId: 0
      },
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*是否打开编辑弹窗*/
      open_basic: false,
      /*当前编辑的对象*/
      userModel: {},
      /*等级*/
      gradeList: [],
      open_tag: false,
      /*所有标签*/
      allTag: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        }
      }
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.formInline;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      UserApi.userlist(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.userList.records;
        self.totalDataNumber = res.data.userList.total;
        self.gradeList = res.data.gradeList;
        self.allTag = res.data.tagList;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*搜索查询*/
    onSubmit() {
      let self = this;
      self.loading = true;
      self.curPage = 1;
      self.getTableList();
    },
    /*打开添加*/
    rechargeClick(item) {
      this.userModel = item;
      this.open_add = true;
    },
    /*打开添加*/
    addClick(item) {
      this.$router.push("/user/user/add");
    },
    /*打开编辑*/
    editClick(item) {
      this.userModel = item;
      this.open_edit = true;
    },
    /* 打开编辑基本信息 */
    editBasicClick(item) {
      let self = this;
      let params = JSON.stringify(item);
      this.$router.push({
        path: "/user/user/edit",
        query: {
          data: params
        }
      });
      // this.userModel = item;
      // this.open_basic = true;
    },
    tagClick(item) {
      this.userModel = item;
      this.open_tag = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e && e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e && e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "tag") {
        this.open_tag = e.openDialog;
        if (e && e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "basic") {
        this.open_basic = e.openDialog;
        if (e && e.type == "success") {
          this.getTableList();
        }
      }
    },
    /*删除用户*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm($t("user.delete_confirm"), $t("user.alert"), {
        confirmButtonText: $t("user.confirm_action"),
        cancelButtonText: $t("user.cancel_button"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        UserApi.deleteuser({
          userId: row.userId
        }, true).then(data => {
          self.loading = false;
          if (data.code == 1) {
            ElMessage({
              message: $t("user.delete_user_success"),
              type: "success"
            });
            self.getTableList();
          } else {
            self.loading = false;
          }
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {
        self.loading = false;
      });
    }
  }
};
</script>

